class Solution {
public:
    bool isGoodNum(int n)
    {
        bool flag=false;
        while(n)
        {
            int tmp=n%10;
            n/=10;
            if(tmp==2 || tmp==5 || tmp==6 || tmp==9)
                flag=true;
            else if(tmp==0|| tmp==1 || tmp==8)
                continue;
            else
                return false;
        }
        return flag;
    }
    int rotatedDigits(int n) {
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=isGoodNum(i);
        }
        return ans;
    }
};